// pages/product/list/list.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        filter: {
            keywords: "",
            brandId: "",
            categoryId: "",
            types: [1], // 1 人气 2 服务 3 黑标
            goodsOnState: 1,
            goodsSortByEnum: 1,
            currenetPageIndex: 1,
            pageSize: 10,
            recordCount: 0
        },
        loading: false,
        hasMore: false,
        status: 0,
        typeId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.setData({
            "filter.types": options.type ? [Number(options.type)] : [1,3],
            "filter.categoryId": options.id ? options.id : ''
        })
        this.reload();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log(this.data.hasMore, 'hasmore')
        if (this.data.hasMore) {
            this.setData({
                'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
            });

            this.getList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    reload() {
        this.getList();
    },

    // 获取商品列表
    async getList() {
        try {
            let res = await api.goods.getIndexPage(this.data.filter),
                data = res.data.ResData || {},
                list = data.Items || [];

            //多页
            if (this.data.filter.currenetPageIndex > 1) {
                list = this.data.list.concat(list);
            }

            list.map(item => {
                if (item.MarketingTypes.toString().indexOf('2') != -1) {
                    item.isSeckill = true;
                } else {
                    item.isSeckill = false;
                }
                if (item.MarketingTypes.toString().indexOf('3') != -1) {
                    item.isGroup = true;
                } else {
                    item.isGroup = false;
                }
            })

            this.setData({
                list,
                loading: false,
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })
            console.log(res, '商品列表')
        } catch (e) {
            console.log(e, '获取商品列表失败')
            this.setData({
                status: 1
            })
        }
    }
})